<?php
/**
 * Created by PhpStorm.
 * User: WUHENG
 * Date: 2022/10/4
 * Time: 11:05
 */

namespace app\index\controller;


use app\index\model\Article;
use app\index\model\Collect;
use app\index\model\Comments;
use app\index\model\Follow;
use app\index\model\Praise;
use app\index\model\Type;
use app\index\model\User;
use app\index\model\Userinfo;
use think\cache\driver\Redis;
use think\Controller;

class Index extends Controller
{
    protected function initialize()
    {
        header("Access-Control-Allow-Origin: *");
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization, x-access-sign, x-access-time");
        if(request()->isOptions())
        {
            exit();
        }
    }

    public function userSign(){
        $data = input('post.');
        $data['userPass']=md5(md5($data['userPass']));
        $data['dateline']= $time = date('y-m-d');
        $res = User::where('userPhone',$data['userPhone'])->find();
        if($res == ''){
            $user = new User();
            $user->save($data);
            if($user){
                return '1';
            }else{
                return 'sb';
            }
        }else{
            return 0;
        }
    }
    public function userLogin(){
        $data = input('post.');
        $data['userPass']=md5(md5($data['userPass']));
        $res = User::where('userPhone',$data['userPhone'])->find();
        if($res['userState'] == 0){
            return 3;
        }
        if($res != ''){
            if($data['userPass'] == $res['userPass']){
                $guid = com_create_guid();
                $redis = new Redis();
                $redis->set($guid,$res);
                return json_encode(['key'=>$guid,'val'=>$res]);
            }else{
                return 2;
            }
        }else{
            return 0;
        }
    }
    public function getUserInfo(){
        $data = input('post.');
        $redis = new Redis();
        $vue = $redis->get($data['res']);
        if ($vue == ''){
            return 0;
        }else{
            return json_encode($vue);
        }
    }
    public function delUserInfo(){
        $data = input('post.');
        $redis = new Redis();
        $del = $redis->rm($data['res']);
        if ($del){
            return 1;
        }else{
            return 0;
        }
    }
    public function redisSever(){
        $redis = new Redis();
        $name = $redis->get("name");
        echo com_create_guid();
    }
    public function getArticle(){
        $data = Type::where('tID',0)->select();
        for ($i=0;$i<count($data);$i++){
            $data[$i]['nextType']=Type::where('tID',$data[$i]['typID'])->select();
            for ($j=0;$j<count($data[$i]['nextType']);$j++){
                $data[$i]['nextType'][$j]['article']=Article::where('typID',$data[$i]['nextType'][$j]['typID'])->select();
            }
        }
        return json_encode($data);
//        dump($data);
    }
    public function articleHot(){
        $num = input('post.id');
        if($num==0){
            $num = '文章';
        }
        if($num==1){
            $num = '散文';
        }
        if($num==2){
            $num = '诗词';
        }
        if($num==3){
            $num = '小说';
        }
        $id = Type::field('typID')->where('typName',$num)->find();
        $data1 = Article::where('readNum','neq',0)->order('readNum','DESC')->select();
        $data2 = Article::where('pushNum','neq',0)->order('pushNum','DESC')->select();
        foreach ($data1 as $v){
            $v->articleType;
        }
        foreach ($data2 as $k){
            $k->articleType;
        }
        $vue = array();
        $res = array();
        for($i=0;$i<count($data1);$i++){
            $vue[$i]['artAuthor']=$data1[$i]['artAuthor'];
            $vue[$i]['artContent']=$data1[$i]['artContent'];
            $vue[$i]['artID']=$data1[$i]['artID'];
            $vue[$i]['artPhoto']=$data1[$i]['artPhoto'];
            $vue[$i]['artState']=$data1[$i]['artState'];
            $vue[$i]['artTitle']=$data1[$i]['artTitle'];
            $vue[$i]['colNum']=$data1[$i]['colNum'];
            $vue[$i]['comtNum']=$data1[$i]['comtNum'];
            $vue[$i]['dateline']=$data1[$i]['dateline'];
            $vue[$i]['praNum']=$data1[$i]['praNum'];
            $vue[$i]['pushNum']=$data1[$i]['pushNum'];
            $vue[$i]['readNum']=$data1[$i]['readNum'];
            $vue[$i]['typID']=$data1[$i]['typID'];
            $vue[$i]['typName']=$data1[$i]['articleType'][0]['typName'];
        }
        for($j=0;$j<count($data2);$j++){
            $res[$j]['artAuthor']=$data2[$j]['artAuthor'];
            $res[$j]['artContent']=$data2[$j]['artContent'];
            $res[$j]['artID']=$data2[$j]['artID'];
            $res[$j]['artPhoto']=$data2[$j]['artPhoto'];
            $res[$j]['artState']=$data2[$j]['artState'];
            $res[$j]['artTitle']=$data2[$j]['artTitle'];
            $res[$j]['colNum']=$data2[$j]['colNum'];
            $res[$j]['comtNum']=$data2[$j]['comtNum'];
            $res[$j]['dateline']=$data2[$j]['dateline'];
            $res[$j]['praNum']=$data2[$j]['praNum'];
            $res[$j]['pushNum']=$data2[$j]['pushNum'];
            $res[$j]['readNum']=$data2[$j]['readNum'];
            $res[$j]['typID']=$data2[$j]['typID'];
            $res[$j]['typName']=$data2[$j]['articleType'][0]['typName'];

        }
        return json_encode(['data'=>$vue,'res'=>$res,'num'=>$num]);
    }
    public function articleList(){
        $id = input('post.id');
        $data = Article::where('typID',$id)->select();
        $res = Type::where('typID',$id)->find();
        return json_encode(['data'=>$data,'res'=>$res]);
    }
    public function getArtInfo(){
        $id = input('post.');
        $redis = new Redis();
        $user = $redis->get($id['redis']);
        $collect = Collect::where(['artID'=>$id['id'],'userID'=>$user['uID']])->find();
        $praise = Praise::where(['artID'=>$id['id'],'userID'=>$user['uID']])->find();
        if($collect){
            $a = 1;
        }else{
            $a = 0;
        }
        if($praise){
            $b = 1;
        }else{
            $b = 0;
        }
        $data = Article::where('artID',$id['id'])->find();
        $photo = User::field('useAavatar')->where('uID',$data['uID'])->find();
        $num =count(Article::where('artAuthor',$data['artAuthor'])->select());
        $vue = Type::where('typID',$data['typID'])->find();
        $res = Type::where('typID',$vue['tID'])->find();
        return json_encode(['data'=>$data,'vue'=>$vue,'res'=>$res,'num'=>$num,'col'=>$a,'pra'=>$b,'photo'=>$photo]);
    }
    public function aboutArt(){
        $id = input('post.id');
        $data = Article::where('artID',$id)->find();
        $res = Article::where('typID',$data['typID'])->order('readNum','DESC')->paginate(7);
        return json_encode($res);
    }
    public function comment(){
        $data = input('post.');
        $redis = new Redis();
        $vue = $redis->get($data['voidInfo']);
        $user = new Comments();
        $user->save([
            'uID'  =>  $vue['uID'],
            'artID'  =>  $data['id'],
            'hID'  =>  $data['uID'],
            'comContent' =>  $data['tarea']
        ]);
        if($user){
            $art = Article::where('artID',$data['id'])->find();
            $num = $art['comtNum']+1;
            $vue = new Article();
            $vue->save(['comtNum'=>$num],['artID'=>$data['id']]);
            return 1;
        }else{
            return 0;
        }
    }
    public function getComment(){
        $id = input('post.');
        $count = count(Comments::where('artID',$id['id'])->select());
        $data = Comments::where(['artID'=>$id['id'],'hID'=>0])->select();
        for($i=0;$i<count($data);$i++){
            $res[$i] = Comments::where(['artID'=>$id['id'],'hID'=>$data[$i]['comtID']])->select();
            foreach ($res[$i] as $v){
                $v->user;
            }
            $data[$i]['res'] = $res[$i];
        }

        foreach ($data as $v){
            $v->user;
        }
        return json_encode(['data'=>$data,'count'=>$count]);
    }
    public function selectType(){
        $type = Type::where('typName','读者创作')->select();
        $vue = Type::where('tID',$type[0]['typID'])->select();
        return json_encode($vue);
    }
    public function userUploadArt(){
        $data = input('post.');
        $redis = new Redis();
        $vue = $redis->get($data['res']);
        $time = date('y-m-d');
        $user = new Article();
        $user->save([
            'typID'  => $data['type'],
            'artTitle' => $data['title'],
            'artAuthor' => $data['author'],
            'artContent' => $data['content'],
            'uID' => $vue['uID'],
            'dateline' => $time
        ]);
        if($user){
            return 1;
        }else{
            return 0;
        }
//        return json_encode($data);
    }
    public function collect(){
        $data = input('post.');
        $time = date('y-m-d');
        $redis = new Redis();
        $vue = $redis->get($data['redis']);
        $res = Collect::where(['artID'=>$data['e'],'userID'=>$vue['uID']])->find();
        if(!$res){
            $user = new Collect();
            $user->save([
                'userID' => $vue['uID'],
                'artID' => $data['e'],
                'dateline' => $time
            ]);
            $art = Article::where('artID',$data['e'])->find();
            $num = $art['colNum']+1;
            $mysql = new Article();
            $mysql->save(['colNum'=>$num],['artID'=>$data['e']]);
            return 1;
        }else{
            return 0;
        }
    }
    public function praise(){
        $data = input('post.');
        $time = date('y-m-d');
        $redis = new Redis();
        $vue = $redis->get($data['redis']);
        $res = Praise::where(['artID'=>$data['e'],'userID'=>$vue['uID']])->find();
        if(!$res){
            $user = new Praise();
            $user->save([
                'userID' => $vue['uID'],
                'artID' => $data['e'],
                'dateline' => $time
            ]);
            $art = Article::where('artID',$data['e'])->find();
            $num = $art['praNum']+1;
            $mysql = new Article();
            $mysql->save(['praNum'=>$num],['artID'=>$data['e']]);
            return 1;
        }else{
            return 0;
        }
    }
    public function getCollect(){
        $data = input('post.');
        $redis = new Redis();
        $vue = $redis->get($data['id']);
        $collect = Collect::where('userID',$vue['uID'])->order('dateline','DESC')->select();
        foreach($collect as $v){
            $v->colArt;
        }
        return json_encode($collect);
    }
    public function noCollect(){
        $data = input('post.');
        $redis = new Redis();
        $vue = $redis->get($data['redis']);
        $collect = Collect::where(['artID'=>$data['id'],'userID'=>$vue['uID']])->delete();
        if($collect){
            $art = Article::where('artID',$data['id'])->find();
            $num = $art['colNum']-1;
            $mysql = new Article();
            $mysql->save(['colNum'=>$num],['artID'=>$data['id']]);
            return 1;
        }else{
            return 0;
        }

    }
    public function follow(){
        $data = input('post.');
        $time = date('y-m-d');
        $redis = new Redis();
        $vue = $redis->get($data['redis']);
        $ad = Follow::where('uID',$data['uID'])->select();
        if(count($ad)>0){
            return 2;
        }
        if($data['uID'] == $vue['uID']){
            return 0;
        }else{
            $user = new Follow();
            $user->save([
               'uID' => $data['uID'],
               'fuID' => $vue['uID'],
               'dateline' => $time
            ]);
            return 1;
        }
//        return json_encode($vue);
    }
    public function getFollow(){
        $data = input('post.');
        $redis = new Redis();
        $vue = $redis->get($data['id']);
        $follow = Follow::where('fuID',$vue['uID'])->order('dateline','DESC')->select();
        foreach($follow as $v){
            $v->getUser;
        }
        return json_encode($follow);
    }
    public function noFollow(){
        $data = input('post.');
        $redis = new Redis();
        $vue = $redis->get($data['redis']);
        $follow = Follow::where(['uID'=>$data['id'],'fuID'=>$vue['uID']])->delete();
        if($follow){
            return 1;
        }else{
            return 0;
        }
    }
    public function getMyArt(){
        $data = input('post.');
        $redis = new Redis();
        $vue = $redis->get($data['id']);
        $myArt = Article::where('uID',$vue['uID'])->order('dateline','DESC')->select();
        foreach($myArt as $v){
            $v->articleType;
        }
        return json_encode($myArt);
    }
    public function myArtDel(){
        $data = input('post.');
        $redis = new Redis();
        $vue = $redis->get($data['redis']);
        $ArtDel = Article::where(['artID'=>$data['id'],'uID'=>$vue['uID']])->delete();
        if($ArtDel){
            return 1;
        }else{
            return 0;
        }
    }
    public function getArtDown(){
        $data = input('post.');
        $redis = new Redis();
        $vue = $redis->get($data['id']);
        $ArtDown = Article::where(['artState'=>0,'uID'=>$vue['uID']])->select();
        foreach($ArtDown as $v){
            $v->articleUser;
        }
        return json_encode($ArtDown);
    }
    public function getAavatar(){
        $data = input('post.');
        $redis = new Redis();
        $vue = $redis->get($data['redis']);
        $res = User::where('uID',$vue['uID'])->find();
        return json_encode($res);
    }
    public function search(){
        $data = input('post.keywords');
        $keywords=$data;
        if($keywords){
            $map=['artTitle','like','%'.$keywords.'%'];
            $map1=['artContent','like','%'.$keywords.'%'];
            $map2=['artAuthor','like','%'.$keywords.'%'];
            $result = Article::whereOr([$map,$map1,$map2])->where('artState',1)->order('readNum','DESC')->select();
            foreach ($result as $v){
                $v->articleType;
            }
            return json_encode($result);
        }
    }
    public function getMyInfo(){
        $data = input('post.');
        $redis = new Redis();
        $vue = $redis->get($data['redis']);
        $res = Userinfo::where('uID',$vue['uID'])->find();
        return json_encode($res);
    }
    public function SaveMyInfo(){
        $data = input('post.');
        $time = date('y-m-d');
        $redis = new Redis();
        $vue = $redis->get($data['redis']);
        $res = Userinfo::where('uID',$vue['uID'])->find();
        if(empty($res)){
            $user = new Userinfo();
            $user->save([
                'dateline' => $time,
                'name' => $data['usr'],
                'uID' => $vue['uID'],
                'sex' => $data['radio'],
                'birth' => $data['month'],
                'place' => $data['place'],
                'sign' => $data['tarea']
            ]);
            if ($user){
                return 1;
            }else{
                return 0;
            }
        }else{
            $user = new Userinfo();
            $user->save([
                'dateline' => $time,
                'name' => $data['usr'],
                'sex' => $data['radio'],
                'birth' => $data['month'],
                'place' => $data['place'],
                'sign' => $data['tarea']
            ],['uID'=>$vue['uID']]);
            if ($user){
                return 1;
            }else{
                return 0;
            }
        }
    }
    public function changePass(){
        $data = input('post.');
        $data['newpass']=md5(md5($data['newpass']));
        $redis = new Redis();
        $vue = $redis->get($data['redis']);
        $user = new User();
        $user->save([
            'userPass' => $data['newpass']
        ],['uID'=>$vue['uID']]);
        if ($user){
            return 1;
        }else{
            return 0;
        }
    }
    public function userPhotoUp()
    {
        $data = input('get.');
        $redis = new Redis();
        $vue = $redis->get($data['id']);
        $file = request()->file('image');
        $info = $file->validate(['ext' => 'jpg,png,gif'])->move('./static/uploads');
        return json_encode($info->getSaveName());
//        $user = new User();
//        $user->save(['useAavatar' => $info->getSaveName()], ['uID' => $vue['uID']]);
//        if($user){
//            return 1;
//        }

    }
    public function PhotoUp(){
        $data = input('post.');
        $redis = new Redis();
        $vue = $redis->get($data['res']);
        $user = new User();
        $user->save(['useAavatar' =>$data['b']], ['uID' => $vue['uID']]);
        if($user){
            return 1;
        }else{
            return 0;
        }
//        return json_encode($data['b']);
    }
    public function getAuthor(){
        $id = input('post.id');
        $data = Userinfo::where('uID',$id)->select();
        foreach ($data as $v){
            $v->getUser;
        }
        return json_encode($data);
    }
    public function test(){
        $data = input("post.");
        $vue = Article::limit($data['page']-1,$data['size'])->select();
        $total = Article::count();
        return json_encode(["data"=>$vue,"total"=>$total]);
    }




























}